import requests
import json



def getToken():
    url = "https://limsuat.midea.com/LIMS-Server/MPLM_Service_Gateway/TokenGenerator/getToken"
    headers = {
        "__TokenAuthorization_UserName_": "cuiml10",
        "__TokenAuthorization_Function_": "getTestForm"
    }

    try:
        response = requests.post(url, headers=headers)
        print("状态码:", response.status_code)
        print("响应内容:", response.text)
        token = response.json().get("token", "")
        print("提取到的token:", token)
        data = json.loads(response.text)
        # 提取token值
        token = data.get("token", "")
        print("提取到的token:", token)
        return token
    except Exception as e:
        print("请求出错:", str(e))


url = "http://localhost:8001/LIMS-Server/MPLM_Service_Gateway/REFLimsRestfulController/queryPlanedTestformItem"
headers = {
    "Content-Type": "application/json",  # 设置请求头为JSON格式
    "__TokenAuthorization_UserName_": "cuiml10",
    "__TokenAuthorization_Function_": "getTestForm",
    "__TokenAuthorization_UID_":getToken()
}
data = {
    "time": "2023-01-13"  # 请求体参数
}

try:
    # 发送POST请求
    response = requests.post(
        url,
        headers=headers,
        json=data  # 使用json参数自动序列化为JSON字符串
    )

    # 打印响应信息
    print("状态码:", response.status_code)
    print("响应内容:", response.text)

except requests.exceptions.RequestException as e:
    print("请求出错:", str(e))
except json.JSONDecodeError as e:
    print("JSON解析出错:", str(e))
